package com.pxst.bank.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pxst.bank.entity.SysBankCardEntity;
import com.pxst.bank.req.AccountGroupReq;
import com.pxst.bank.req.BankCardGroupReq;
import com.pxst.bank.resp.AccountGroupResp;
import com.pxst.bank.resp.BankCardGroupResp;
import com.pxst.merchant.entity.SysMerchantInfo;
import com.pxst.merchant.mapper.SysMerchantInfoMapper;
import com.pxst.resp.ApiResp;
import icu.mhb.mybatisplus.plugln.base.service.impl.JoinServiceImpl;
import icu.mhb.mybatisplus.plugln.core.JoinLambdaWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pxst.bank.entity.SysBankCardGroupEntity;
import com.pxst.bank.mapper.SysBankCardGroupMapper;
import com.pxst.bank.service.SysBankCardGroupService;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yangxy
 * @version 创建时间：2023年9月7日 下午4:04:46
 */
@Service
public class SysBankCardGroupServiceImpl extends JoinServiceImpl<SysBankCardGroupMapper, SysBankCardGroupEntity> implements SysBankCardGroupService {

    @Autowired
    private SysBankCardGroupMapper sysBankCardGroupMapper;

    @Autowired
    private SysMerchantInfoMapper sysMerchantInfoMapper;

    /**
     * 群组下拉框信息
     *
     * @return
     */
    @Override
    public ApiResp<BankCardGroupResp> getInfo() {
        List<SysBankCardGroupEntity> sysBankCardGroupEntities = sysBankCardGroupMapper.selectList(null);
        List<BankCardGroupResp> collect = sysBankCardGroupEntities.stream().map(iter -> {
            BankCardGroupResp bankCardGroupResp = new BankCardGroupResp();
            BeanUtils.copyProperties(iter, bankCardGroupResp);
            bankCardGroupResp.setBankGroupId(iter.getId());
            return bankCardGroupResp;
        }).collect(Collectors.toList());
        return ApiResp.sucess(collect);
    }

    /**
     * 账户群组搜索
     *
     * @param bankCardGroupReq
     * @return
     */
    @Override
    public ApiResp<SysBankCardGroupEntity> getGroupList(BankCardGroupReq bankCardGroupReq) {

        JoinLambdaWrapper<SysBankCardGroupEntity> jlw = new JoinLambdaWrapper<>(SysBankCardGroupEntity.class);
        jlw.eq(StringUtils.hasText(bankCardGroupReq.getCurrency()), SysBankCardGroupEntity::getCurrency, bankCardGroupReq.getCurrency())
                .eq(bankCardGroupReq.getBankGroupId() != null, SysBankCardGroupEntity::getId, bankCardGroupReq.getBankGroupId())
                .eq(bankCardGroupReq.getGroupType() != null, SysBankCardGroupEntity::getGroupType, bankCardGroupReq.getGroupType())
                .distinct();

        jlw.leftJoin(SysMerchantInfo.class, SysMerchantInfo::getBankGroupId, SysBankCardGroupEntity::getId)
                .in(bankCardGroupReq.getMerchanId() != null && bankCardGroupReq.getMerchanId().size() > 0,
                        SysMerchantInfo::getId, bankCardGroupReq.getMerchanId())
                .end();

        jlw.leftJoin(SysBankCardEntity.class, SysBankCardEntity::getBankCardGroupId, SysBankCardGroupEntity::getId)
                .in(bankCardGroupReq.getBankCardId() != null && bankCardGroupReq.getBankCardId().size() > 0,
                        SysBankCardEntity::getId, bankCardGroupReq.getBankCardId())
                .end();

        PageHelper.startPage(bankCardGroupReq.getPage(), bankCardGroupReq.getRp());
        Page<SysBankCardGroupEntity> page = (Page<SysBankCardGroupEntity>) sysBankCardGroupMapper.joinSelectList(jlw, SysBankCardGroupEntity.class);
        return ApiResp.page(page);
    }

    /**
     * 账户群组下所有商户信息
     *
     * @param id
     * @return
     */
    @Override
    public ApiResp<List<SysMerchantInfo>> getMerchantAll(Long id) {
        List<SysMerchantInfo> sysMerchantInfos = sysMerchantInfoMapper.selectList(new LambdaQueryWrapper<SysMerchantInfo>().eq(SysMerchantInfo::getBankGroupId, id));
        return ApiResp.sucess(sysMerchantInfos);
    }

    @Override
    public ApiResp searchAccountGroup(AccountGroupReq accountGroupReq) {
        JoinLambdaWrapper<SysBankCardGroupEntity> jlw = new JoinLambdaWrapper<>(SysBankCardGroupEntity.class);
        jlw
                .eq(!ObjectUtils.isEmpty(accountGroupReq.getBankCardGroupId()), SysBankCardGroupEntity::getId, accountGroupReq.getBankCardGroupId())
                .eq(!ObjectUtils.isEmpty(accountGroupReq.getCurrency()), SysBankCardGroupEntity::getCurrency, accountGroupReq.getCurrency())
                .selectAs(sbcge -> {
                            sbcge.addFunAlias(SysBankCardGroupEntity::getId, AccountGroupResp::getBankCardGroupId);
                            sbcge.addFunAlias(SysBankCardGroupEntity::getGroupName, AccountGroupResp::getBankCardGroupName);
                            sbcge.addFunAlias(SysBankCardGroupEntity::getCurrency, AccountGroupResp::getCurrency);
                            sbcge.addFunAlias(SysBankCardGroupEntity::getCreator, AccountGroupResp::getCreator);
                            sbcge.addFunAlias(SysBankCardGroupEntity::getUpdater, AccountGroupResp::getUpdater);
                            sbcge.addFunAlias(SysBankCardGroupEntity::getCreatedAt, AccountGroupResp::getCreatedAt);
                            sbcge.addFunAlias(SysBankCardGroupEntity::getUpdatedAt, AccountGroupResp::getUpdatedAt);
                        }
                );

        jlw
                .leftJoin(SysMerchantInfo.class, SysMerchantInfo::getBankGroupId, SysBankCardGroupEntity::getId)
                .in(!ObjectUtils.isEmpty(accountGroupReq.getMerchantId()), SysMerchantInfo::getId, accountGroupReq.getMerchantId())
                .end();

        jlw
                .leftJoin(SysBankCardEntity.class, SysBankCardEntity::getBankCardGroupId, SysBankCardGroupEntity::getId)
                .in(!ObjectUtils.isEmpty(accountGroupReq.getBankCardId()), SysBankCardEntity::getId, accountGroupReq.getBankCardId())
                .eq(!ObjectUtils.isEmpty(accountGroupReq.getMode()), SysBankCardEntity::getMode, accountGroupReq.getMode())
                .eq(!ObjectUtils.isEmpty(accountGroupReq.getAccountCode()), SysBankCardEntity::getAccountCode, accountGroupReq.getAccountCode())
                .selectAs(sbce -> {
                    sbce.addFunAlias(SysBankCardEntity::getMode, AccountGroupResp::getMode);
                })
                .end();


        jlw
                .orderByDesc(SysBankCardGroupEntity::getCreatedAt);

        PageHelper.startPage(accountGroupReq.getPage(), accountGroupReq.getRp());
        Page<AccountGroupResp> page = (Page<AccountGroupResp>) sysBankCardGroupMapper.joinSelectList(jlw, AccountGroupResp.class);
        return ApiResp.page(page);
    }
}
